<?php
require 'weixin.config.php';

if (strtolower($_SERVER['REQUEST_METHOD']) == 'get') {
    //开发者提交配置
    $signatrue = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];
    $params = array(TOKEN, $timestamp, $nonce);
    sort($params);
    $sig = sha1(implode($params));
    if ($signatrue == $sig) {
        $echo = $_GET['echostr'];
        echo $echo;
    } else {
        throw new Exception("签名不正确");
    }
} elseif (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
    include 'ResponseMsg.php';
    //微信服务器提交到本服务器的post请示，并携带了xml数据
    $rawData = $GLOBALS['HTTP_RAW_POST_DATA'];
    $postdata = simplexml_load_string($rawData, 'SimpleXMLElement', LIBXML_NOCDATA);
    //以上代码实现将xml数据转化为php可访问对象
    $msgtype = $postdata->MsgType;
    if ($msgtype == "text") {
        $content = $postdata->Content;//取出用户发送的内容
        $resptext = "";//回复的内容
        if ($content == "m") {
            $resptext = "1.十九大\n";
            $resptext = $resptext . "2.java应用开发\n";
            $resptext = $resptext . "3.cocos游戏开\n";
            $resptext = $resptext . "4.微信开发\n";
            $resptext = $resptext . "5.news\n";
            $resptext = $resptext . "6.music\n";
        } else {
            switch ($content) {
                case "1":
                    $resptext = "阅读十九大";
                    break;
                case "2":
                    $resptext = "java应用开发";
                    break;
                case "3":
                    $resptext = "cocos游戏开发";
                    break;
                case "4":
                    $resptext = "微信开发";
                    break;
                case "5":
                    ResponseMsg::responseNews($postdata);
                    break;
                case "6":
                    ResponseMsg::outputMusic($postdata);
                    break;
                default:
                    $resptext = "请输入m获取菜单";
            }
        }
        ResponseMsg::responseText($postdata, $resptext);

    } elseif ($msgtype == "image") {
        $mediaid = $postdata->MediaId;
        ResponseMsg::responseImage($postdata, $mediaid);
    } elseif ($msgtype == 'location') {
        //用户发送了定位
        $x = $postdata->Location_X;
        $y = $postdata->Location_Y;
        $text = "你当前位置:\n东经:" . $y . "\n北纬:" . $x;
        ResponseMsg::responseLocation($postdata, $text);
    }
}



